<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta content="width=device-width, initial-scale=1.0" name="viewport">
    <title>IVT绩效考核系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
            background-color: #f5f5f5;
            color: #333;
        }

        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            text-align: center;
            padding: 20px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }

        .header h1 {
            font-size: 24px;
            font-weight: 600;
        }

        .user-info {
            background: white;
            margin: 20px;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }

        .menu-container {
            margin: 20px;
        }

        .menu-item {
            background: white;
            margin-bottom: 15px;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            cursor: pointer;
            transition: transform 0.2s, box-shadow 0.2s;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }

        .menu-item:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 20px rgba(0,0,0,0.15);
        }

        .menu-item .icon {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 20px;
            margin-right: 15px;
        }

        .menu-item .content {
            flex: 1;
        }

        .menu-item .title {
            font-size: 18px;
            font-weight: 600;
            margin-bottom: 5px;
        }

        .menu-item .desc {
            font-size: 14px;
            color: #666;
        }

        .menu-item .arrow {
            color: #ccc;
            font-size: 16px;
        }

        .dept-icon { background: #e3f2fd; color: #1976d2; }
        .teacher-icon { background: #f3e5f5; color: #7b1fa2; }
        .done-icon { background: #e8f5e8; color: #388e3c; }

        .loading {
            text-align: center;
            padding: 50px;
            color: #666;
        }

        .error {
            background: #ffebee;
            color: #c62828;
            padding: 15px;
            margin: 20px;
            border-radius: 8px;
            text-align: center;
        }

        /* 登录界面样式 */
        .login-container {
            padding: 40px 20px;
            max-width: 400px;
            margin: 0 auto;
        }

        .login-header {
            text-align: center;
            margin-bottom: 40px;
        }

        .logo {
            font-size: 48px;
            margin-bottom: 15px;
        }

        .login-title {
            font-size: 24px;
            font-weight: 600;
            color: #333;
            margin-bottom: 5px;
        }

        .login-subtitle {
            font-size: 16px;
            color: #666;
        }

        .login-methods {
            margin-bottom: 30px;
        }

        .method-item {
            background: white;
            border-radius: 12px;
            padding: 20px;
            margin-bottom: 15px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.08);
            cursor: pointer;
            transition: all 0.3s ease;
            display: flex;
            align-items: center;
            border: 2px solid transparent;
        }

        .method-item:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 20px rgba(0,0,0,0.12);
            border-color: #667eea;
        }

        .method-icon {
            font-size: 24px;
            margin-right: 15px;
            width: 40px;
            text-align: center;
        }

        .method-content {
            flex: 1;
        }

        .method-title {
            font-size: 16px;
            font-weight: 600;
            color: #333;
            margin-bottom: 5px;
        }

        .method-desc {
            font-size: 14px;
            color: #666;
        }

        .method-arrow {
            font-size: 18px;
            color: #ccc;
        }

        .weixin-login {
            background: linear-gradient(135deg, #1aad19 0%, #00d100 100%);
            color: white;
        }

        .weixin-login .method-title,
        .weixin-login .method-desc {
            color: white;
        }

        .weixin-login .method-arrow {
            color: rgba(255,255,255,0.8);
        }

        .login-info {
            text-align: center;
            color: #666;
            font-size: 14px;
            line-height: 1.6;
        }

        .login-info p {
            margin-bottom: 8px;
        }

        /* 手动登录表单样式 */
        .manual-form {
            padding: 20px;
        }

        .form-header {
            display: flex;
            align-items: center;
            margin-bottom: 30px;
            padding-bottom: 15px;
            border-bottom: 1px solid #f0f0f0;
        }

        .form-header h3 {
            flex: 1;
            text-align: center;
            font-size: 18px;
            color: #333;
            margin: 0;
        }

        .back-btn {
            background: none;
            border: none;
            font-size: 20px;
            color: #667eea;
            cursor: pointer;
            padding: 5px;
            margin-right: 15px;
        }

        .form-content {
            margin-bottom: 30px;
        }

        .input-group {
            margin-bottom: 20px;
        }

        .input-group label {
            display: block;
            margin-bottom: 8px;
            font-size: 14px;
            font-weight: 500;
            color: #333;
        }

        .input-group input {
            width: 100%;
            padding: 12px 16px;
            border: 2px solid #e0e0e0;
            border-radius: 8px;
            font-size: 16px;
            transition: border-color 0.3s ease;
        }

        .input-group input:focus {
            outline: none;
            border-color: #667eea;
        }

        .login-btn {
            width: 100%;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 8px;
            padding: 15px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .login-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
        }

        .login-btn:disabled {
            background: #ccc;
            cursor: not-allowed;
            transform: none;
            box-shadow: none;
        }

        .form-tips {
            color: #666;
            font-size: 13px;
            line-height: 1.5;
        }

        .form-tips p {
            margin-bottom: 5px;
        }

        /* 用户信息区域样式优化 */
        .user-info {
            background: white;
            border-radius: 12px;
            padding: 20px;
            margin: 20px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.05);
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .welcome-text {
            font-size: 16px;
            font-weight: 500;
            color: #333;
        }

        .logout-btn {
            background: #f5f5f5;
            color: #666;
            border: none;
            border-radius: 6px;
            padding: 8px 16px;
            font-size: 14px;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .logout-btn:hover {
            background: #e0e0e0;
            color: #333;
        }


    </style>
</head>
<body>
<div class="header">
    <h1>IVT绩效考核系统</h1>
</div>

<div class="loading" id="loading" style="display:none;">
    正在验证用户信息...
</div>

<div class="error" id="error" style="display:none;"></div>

<!-- 教师登录界面 -->
<div class="login-container" id="login-form">
    <div class="login-header">
        <div class="logo">🎓</div>
        <div class="login-title">IVT绩效考核系统</div>
        <div class="login-subtitle">教师登录</div>
    </div>

    <div class="login-methods">
        <div class="method-item weixin-login" onclick="showWeixinLogin()">
            <div class="method-icon">📱</div>
            <div class="method-content">
                <div class="method-title">企业微信登录</div>
                <div class="method-desc">通过企业微信账号登录系统</div>
            </div>
            <div class="method-arrow">›</div>
        </div>

        <div class="method-item manual-login" onclick="showManualLogin()">
            <div class="method-icon">👤</div>
            <div class="method-content">
                <div class="method-title">手动输入登录</div>
                <div class="method-desc">输入企业微信账号登录</div>
            </div>
            <div class="method-arrow">›</div>
        </div>
    </div>

    <div class="login-info">
        <p>🔒 请使用您的企业微信账号登录系统</p>
        <p>📋 登录后可进行部门测评和教师测评</p>
    </div>
</div>

<!-- 手动登录表单 -->
<div class="manual-form" id="manual-login-form" style="display:none;">
    <div class="form-header">
        <button class="back-btn" onclick="backToLoginMethods()">‹</button>
        <h3>企业微信账号登录</h3>
    </div>

    <div class="form-content">
        <div class="input-group">
            <label for="qywxInput">企业微信账号</label>
            <input id="qywxInput" placeholder="请输入您的企业微信账号" type="text" value="GuanHuiLiang"/>
        </div>

        <button class="login-btn" onclick="manualLogin()">
            <span class="btn-text">登录</span>
            <span class="btn-loading" style="display:none;">登录中...</span>
        </button>
    </div>

    <div class="form-tips">
        <p>💡 提示：企业微信账号通常为工号或邮箱前缀</p>
        <p>❓ 如有问题，请联系系统管理员</p>
    </div>
</div>

<div id="main-content" style="display:none;">
    <div class="user-info">
        <div class="welcome-text">欢迎您，<span id="userName"></span></div>
        <div class="user-actions">
            <button class="logout-btn" onclick="logout()">退出登录</button>
        </div>
    </div>

    <div class="menu-container">
        <div class="menu-item" onclick="location.href='dept-list.html'">
            <div class="icon dept-icon">🏢</div>
            <div class="content">
                <div class="title">部门测评</div>
                <div class="desc">对职能部门进行测评打分</div>
            </div>
            <div class="arrow">›</div>
        </div>

        <div class="menu-item" onclick="location.href='teacher-list.html'">
            <div class="icon teacher-icon">👩‍🏫</div>
            <div class="content">
                <div class="title">教师测评</div>
                <div class="desc">对教师进行测评打分</div>
            </div>
            <div class="arrow">›</div>
        </div>

        <div class="menu-item" onclick="showEvaluatedMenu()">
            <div class="icon done-icon">✓</div>
            <div class="content">
                <div class="title">已测评查看</div>
                <div class="desc">查看已完成的测评记录</div>
            </div>
            <div class="arrow">›</div>
        </div>
    </div>
</div>

<script>
        // 从URL参数获取企业微信账号
        function getQueryParam(name) {
            const urlParams = new URLSearchParams(window.location.search);
            return urlParams.get(name);
        }

        // 页面初始化
        function initPage() {
            console.log('页面初始化开始...');

            // 检查是否有企业微信登录参数
            const jsqywxzh = getQueryParam('jsqywxzh');
            console.log('企业微信参数:', jsqywxzh);

            if (jsqywxzh && jsqywxzh.trim() !== '') {
                // 如果有企业微信参数，自动进行验证
                console.log('开始企业微信自动登录...');
                autoLogin(jsqywxzh);
            } else {
                // 检查是否已经登录
                const currentUser = localStorage.getItem('currentUser');
                console.log('本地用户信息:', currentUser);

                if (currentUser) {
                    try {
                        const user = JSON.parse(currentUser);
                        console.log('显示主界面，用户:', user.jsxm);
                        showMainContent(user);
                    } catch (e) {
                        console.log('用户信息解析失败，清除缓存');
                        localStorage.removeItem('currentUser');
                        showLoginForm();
                    }
                } else {
                    console.log('显示登录表单');
                    showLoginForm();
                }
            }
        }

        // 显示登录表单
        function showLoginForm() {
            console.log('显示登录表单');
            document.getElementById('login-form').style.display = 'block';
            document.getElementById('manual-login-form').style.display = 'none';
            document.getElementById('main-content').style.display = 'none';
            document.getElementById('loading').style.display = 'none';
            document.getElementById('error').style.display = 'none';
        }

        // 显示企业微信登录
        function showWeixinLogin() {
            alert('📱 企业微信登录说明：\n\n' +
                  '1. 请使用企业微信扫描二维码或\n' +
                  '2. 在企业微信工作台中找到"绩效考核"应用\n' +
                  '3. 点击进入即可自动登录系统\n\n' +
                  '如需测试，请使用"手动输入登录"功能');
        }

        // 显示手动登录表单
        function showManualLogin() {
            document.getElementById('login-form').style.display = 'none';
            document.getElementById('manual-login-form').style.display = 'block';
            document.getElementById('qywxInput').focus();
        }

        // 返回登录方式选择
        function backToLoginMethods() {
            document.getElementById('manual-login-form').style.display = 'none';
            document.getElementById('login-form').style.display = 'block';
        }

        // 手动登录
        function manualLogin() {
            const qywxInput = document.getElementById('qywxInput');
            const qywx = qywxInput.value.trim();

            if (!qywx) {
                alert('请输入企业微信账号');
                qywxInput.focus();
                return;
            }

            // 显示登录按钮加载状态
            const loginBtn = document.querySelector('.login-btn');
            const btnText = loginBtn.querySelector('.btn-text');
            const btnLoading = loginBtn.querySelector('.btn-loading');

            loginBtn.disabled = true;
            btnText.style.display = 'none';
            btnLoading.style.display = 'inline';

            // 调用登录验证
            performLogin(qywx).finally(() => {
                loginBtn.disabled = false;
                btnText.style.display = 'inline';
                btnLoading.style.display = 'none';
            });
        }

        // 自动登录（企业微信跳转）
        function autoLogin(jsqywxzh) {
            document.getElementById('loading').style.display = 'block';
            document.getElementById('login-form').style.display = 'none';

            performLogin(jsqywxzh);
        }

        // 执行登录验证
        async function performLogin(qywx) {
            console.log('开始登录验证，账号:', qywx);

            try {
                const response = await fetch('/evaluation/api/h5/auth/check', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ qywx: qywx })
                });

                console.log('登录请求状态:', response.status);

                if (!response.ok) {
                    throw new Error(`HTTP ${response.status}: ${response.statusText}`);
                }

                const result = await response.json();
                console.log('登录响应:', result);

                if (result.success) {
                    // 登录成功
                    console.log('登录成功，用户:', result.data.jsxm);
                    localStorage.setItem('currentUser', JSON.stringify(result.data));
                    showMainContent(result.data);
                } else {
                    console.log('登录失败:', result.message);
                    showError(result.message || '登录失败，请检查企业微信账号');
                }
            } catch (error) {
                console.error('登录网络错误:', error);
                showError('网络连接失败，请检查网络连接后重试');
            }
        }

        // 显示主界面
        function showMainContent(user) {
            document.getElementById('userName').textContent = user.jsxm;
            document.getElementById('loading').style.display = 'none';
            document.getElementById('login-form').style.display = 'none';
            document.getElementById('manual-login-form').style.display = 'none';
            document.getElementById('error').style.display = 'none';
            document.getElementById('main-content').style.display = 'block';
        }

        // 显示错误信息
        function showError(message) {
            console.log('显示错误:', message);
            document.getElementById('loading').style.display = 'none';
            document.getElementById('login-form').style.display = 'none';
            document.getElementById('manual-login-form').style.display = 'none';
            document.getElementById('main-content').style.display = 'none';
            document.getElementById('error').style.display = 'block';
            document.getElementById('error').textContent = message;

            // 3秒后返回登录界面
            setTimeout(() => {
                document.getElementById('error').style.display = 'none';
                showLoginForm();
            }, 3000);
        }

        // 退出登录
        function logout() {
            if (confirm('确定要退出登录吗？')) {
                localStorage.removeItem('currentUser');

                // 调用后端退出接口
                fetch('/evaluation/api/h5/auth/logout', {
                    method: 'POST'
                }).catch(error => {
                    console.error('退出登录请求失败:', error);
                });

                showLoginForm();
            }
        }

        // 显示已测评菜单
        function showEvaluatedMenu() {
            const modal = document.createElement('div');
            modal.style.cssText = `
                position: fixed;
                top: 0;
                left: 0;
                right: 0;
                bottom: 0;
                background: rgba(0,0,0,0.5);
                display: flex;
                align-items: center;
                justify-content: center;
                z-index: 1000;
            `;

            modal.innerHTML = `
                <div style="background: white; border-radius: 10px; padding: 20px; margin: 20px; width: 90%; max-width: 300px;">
                    <h3 style="text-align: center; margin-bottom: 20px;">选择查看类型</h3>
                    <div style="margin-bottom: 15px;">
                        <button onclick="location.href='dept-evaluated.html'" style="width: 100%; padding: 15px; background: #667eea; color: white; border: none; border-radius: 8px; font-size: 16px; margin-bottom: 10px;">
                            已测评部门
                        </button>
                    </div>
                    <div style="margin-bottom: 15px;">
                        <button onclick="location.href='teacher-evaluated.html'" style="width: 100%; padding: 15px; background: #7b1fa2; color: white; border: none; border-radius: 8px; font-size: 16px; margin-bottom: 10px;">
                            已测评教师
                        </button>
                    </div>
                    <button onclick="closeModal(this)" style="width: 100%; padding: 10px; background: #ccc; color: #333; border: none; border-radius: 8px; font-size: 14px;">
                        关闭
                    </button>
                </div>
            `;

            document.body.appendChild(modal);

            // 点击遮罩关闭
            modal.addEventListener('click', function(e) {
                if (e.target === modal) {
                    modal.remove();
                }
            });
        }

        // 关闭弹出框
        function closeModal(button) {
            const modal = button.closest('[style*="position: fixed"]');
            if (modal) {
                modal.remove();
            }
        }

        // 页面加载完成后初始化
        document.addEventListener('DOMContentLoaded', function() {
            initPage();
        });


</script>
</body>
</html>
